.section .bss
.comm nums, 4000       @ reserva espacio para numeros 4bits por numero

.section  .data

spc:                    @separarlos con +
        .ascii"    "
len= . - spc

nl:
      .ascii "\n"       @renglon de separacion
      
limit:               @indice del ultimo numero que necesitaremos.
      .long 1000
 
 
 .section .text
.global main /* 'main' is our entry point and must be global */
 
main:
        ldr     r8, =nums
        ldr     r7, =limit
        ldr     r9, [r7]
        str     fp, [sp, #-4]!
        add     fp, sp, #0
        sub     sp, sp, #28
        mov     r3, #100
        str     r3, [fp, #-8]
.L7:
        mov     r3, #0
        str     r3, [fp, #-12]
        mov     r3, #0
        str     r3, [fp, #-16]
        ldr     r3, [fp, #-8]
        str     r3, [fp, #-20]
        mov     r3, #0
        str     r3, [fp, #-24]
        ldr     r3, [fp, #-8]
        ldr     r2, [fp, #-8]
        mul     r3, r2, r3
        str     r3, [fp, #-24]
        b       .L2
.L3:
        ldr     r2, [fp, #-12]
        mov     r3, r2
        mov     r3, r3, asl #2
        add     r3, r3, r2
        mov     r3, r3, asl #1
        mov     r0, r3
        ldr     r1, [fp, #-20]
        ldr     r3, .L8
        smull   r2, r3, r1, r3
        mov     r2, r3, asr #2
        mov     r3, r1, asr #31
        rsb     r2, r3, r2
        mov     r3, r2
        mov     r3, r3, asl #2
        add     r3, r3, r2
        mov     r3, r3, asl #1
        rsb     r2, r3, r1
        add     r3, r0, r2
        str     r3, [fp, #-12]
        ldr     r3, [fp, #-20]
        ldr     r2, .L8
        smull   r1, r2, r3, r2
        mov     r2, r2, asr #2
        mov     r3, r3, asr #31
        rsb     r3, r3, r2
        str     r3, [fp, #-20]
.L2:
        ldr     r3, [fp, #-20]
        cmp     r3, #0
        movle   r3, #0
        movgt   r3, #1
        and     r3, r3, #255
        cmp     r3, #0
        bne     .L3
        b       .L4
.L5:
        ldr     r2, [fp, #-16]
        mov     r3, r2
        mov     r3, r3, asl #2
        add     r3, r3, r2
        mov     r3, r3, asl #1
        mov     r0, r3
        ldr     r1, [fp, #-24]
        ldr     r3, .L8
        smull   r2, r3, r1, r3
        mov     r2, r3, asr #2
        mov     r3, r1, asr #31
        rsb     r2, r3, r2
        mov     r3, r2
        mov     r3, r3, asl #2
        add     r3, r3, r2
        mov     r3, r3, asl #1
        rsb     r2, r3, r1
        add     r3, r0, r2
        str     r3, [fp, #-16]
        ldr     r3, [fp, #-24]
        ldr     r2, .L8
        smull   r1, r2, r3, r2
        mov     r2, r2, asr #2
        mov     r3, r3, asr #31
        rsb     r3, r3, r2
        str     r3, [fp, #-24]
.L4:
        ldr     r3, [fp, #-24]
        cmp     r3, #0
        movle   r3, #0
        movgt   r3, #1
        and     r3, r3, #255
        cmp     r3, #0
        bne     .L5
        ldr     r3, [fp, #-8]
        ldr     r2, [fp, #-12]
        mul     r2, r3, r2
        ldr     r3, [fp, #-16]
        cmp     r2, r3
        bne     .L6
        ldr     r3, [fp, #-8]
        str     r3, [r8, #4]!
.L6:
        ldr     r3, [fp, #-8]
        add     r3, r3, #1
        str     r3, [fp, #-8]
        ldr     r2, [fp, #-8]
        ldr     r3, .L8+4
        cmp     r2, r3
        bge	    P10
        movgt   r3, #0
        movle   r3, #1
        and     r3, r3, #255
        cmp     r3, #0
        bne     .L7
        mov     r3, #0
        mov     r0, r3
        add     sp, fp, #0
        ldmfd   sp!, {fp}
        bx      lr
.L8:
        .word   1717986919
        .word   999
        
        
P10:
 mov r0,$1
 mov r4,$0
 mov r5,$0
 ldr r6,=nums
 ldr r3,[r6]

printLoop:
bl print_num		@ function call
add r4, $1		@ add one to temp counter
add r5, $1		@ add one to counter
cmp r5, r9 		@ are we done?
bge exit		@ if so, exit
cmp r4, $9		@ after 10 primes ...
bgt newline		@ print a newline
ble space		@ add spaces

space:                  @ we jump here if
mov r0, $1              @ we are going to 
ldr r1, =spc		@ print spaces
ldr r2, =len	
mov r7, $4
svc $0
ldr r3, [r6, #4]!	@ load next prime
bal printLoop           @ continue printing

newline:                @ we jump here if
mov r0, $1              @ we are going to
ldr r1, =nl             @ print a newline
mov r2, $1
mov r7, $4
svc $0
ldr r3, [r6, #4]!       @ load next prime
mov r4, $0              @ reset temporary counter
bal printLoop		@ continue printing

@@@@@@@@@@@@@@@@@@@@@@
@ print_num function @
@@@@@@@@@@@@@@@@@@@@@@

print_num:
	stmfd sp!, {r0-r9, lr}	@ push regs to stack
	mov r4, $0 		@ set division counter to zero
	mov r5, $1		@ set char counter to one

loop:				@ division routine
	cmp r3, $9		
	ble stackPush		@ if r3 <= 9, call stackPush
	sub r3, r3, $10		@ else, subtract 10 from r3
	add r4, r4, $1		@ add one to div. counter
	bal loop		@ repeat

stackPush:
	add r5, r5, $1		@ increment char counter
	orr r0, r3, $0x30	@ logical OR - add 48 to digit to get ascii code
	stmfd	sp!, {r0}	@ push onto stack
	cmp r4, $0		@ if the div. counter is zero ...
	beq printChars		@ call print function
	mov r3, r4		@ else, load div. count into r3
	mov r4, $0		@ reset div. counter
	bal loop		@ back to top of loop

printChars:
	mov r1, sp		@ use stack pointer to provide ascii code
	mov r0, $1		@ stdout is file descriptor 1
	mov r2, $1		@ length to print is 1
	mov r7, $4		@ write syscall
	svc $0			@ wake kernel
	subs r5, r5, $1		@ decrement string counter and set flag
	ble return		@ return if done
	ldmfd sp!, {r0}		@ pull next char from from stack 
	bal printChars		@ get next char
return:
	ldmfd sp!, {r0-r9, pc}	@ restore registers

exit:
mov r0, $1			@ print a newline
ldr r1, =nl
mov r2, $1
mov r7, $4
svc $0
mov r7, $1			@ exit
svc $0

.end
